package main {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  import flash.ui.*;
  import flash.net.*;
  import flash.utils.*;
  import caurina.transitions.Tweener;
  
  public class Tooltip extends MovieClip {
    private var _text_field:TextField;
    private var _tooltip_bg:TooltipBg;
    private var _tooltip_bg2:TooltipBg2;
    private var _show_timeout:Number;
    private var _shown:Boolean = false;
    
    public function Tooltip() {
      this.visible = false;
      this.alpha = 0;
      
      _tooltip_bg = new TooltipBg();
      _tooltip_bg2 = new TooltipBg2();
      _tooltip_bg.visible = _tooltip_bg2.visible = false;
      addChild(_tooltip_bg);
      addChild(_tooltip_bg2);
      
      _text_field = new TextField();
      _text_field.width = 100;
      _text_field.multiline = true;
      _text_field.wordWrap = true;
      _text_field.autoSize = TextFieldAutoSize.LEFT;
      _text_field.x = 10;
      _text_field.y = 8;
      
      var tf:TextFormat = new TextFormat();
      tf.font = 'Tahoma,Helvetica,Arial,sans-serif';
      tf.size = 12;
      tf.color = 0x000000;
       _text_field.defaultTextFormat = tf;
      
      addChild(_text_field);
    }
    public function show(x:Number, y:Number, text:String) {
      if (_shown) {
        clearTimeout(_show_timeout);
      }
      _shown = true;
      _text_field.text = text;
      
      if (y < _text_field.textHeight + 34) {
        _tooltip_bg.visible = false;
        _tooltip_bg2.visible = true;
        _tooltip_bg2.width = _text_field.textWidth + 26;
        _tooltip_bg2.height = _text_field.textHeight + 22;
        this.y = y + 18;
      } else {
        _tooltip_bg2.visible = false;
        _tooltip_bg.visible = true;
        _tooltip_bg.width = _text_field.textWidth + 26;
        _tooltip_bg.height = _text_field.textHeight + 34;
        this.y = y - _tooltip_bg.height;
      }
      this.x = x - 16;
      Tweener.addTween(this, {time: 0.7, _autoAlpha:0.9});
    }
    public function hide(timeout:Number = 0) {
      if (!_shown) return;
      if (timeout != 0) {
        clearTimeout(_show_timeout);
        _show_timeout = setTimeout(_hide, timeout);
      } else {
        _hide();
      }
    }
    private function _hide() {
      _shown = false;
      _text_field.text = "";
      Tweener.addTween(this, {time: 0.7, _autoAlpha:0});
    }
  }
}